% load data
[val, text, raw] = xlsread('../data/VNIBOR.xlsx',3)
%save data2
%%
% load data2
close all;
clc;
% Y = val(:,2);
Y = val(:,2);
X = val(:,5:end);
%get the mname of predictors
namePredictors = text(1,6:end);

ctree = fitrtree(X,Y,'CategoricalPredictors',size(X,2),'PredictorNames',namePredictors); 
view(ctree,'mode','graph')

resuberror = resubLoss(ctree,'subtrees','all');
[E,SE,Nleaf,BestLevel] = cvLoss(ctree,'subtrees','all');
[resuberror E]

%%
leafs = logspace(0,2,10);
rng('default')
N = numel(leafs);
err = zeros(N,1);
for n=1:N
    t = fitrtree(X,Y,'CrossVal','On',...
        'MinLeaf',leafs(n));
    err(n) = kfoldLoss(t);
end
plot(leafs,err);
xlabel('Min Leaf Size');
ylabel('cross-validated error');

%%

ctree1 = fitrtree(X,Y,'MinLeaf',10,'CategoricalPredictors',8,'PredictorNames',namePredictors); 
view(ctree,'mode','graph')


%%

newTree = ctree
predictor = [0.01 0.03, 0.05, 0.10, 0,156858.6955,0.044337832,0];
[xnew, score] = predict(newTree,predictor)

%%
ctree1 = prune(ctree,'Level',15)
view(ctree1,'mode','graph') % text description

%%
windowSize = 60; % 5yr
N = size(Y,1);
result_predict = zeros(N - windowSize,2)


% dates = datenum(text(2:end,1))
dates2 = dates(windowSize+1:end)


for i = 1 : N - windowSize
%     X_training = X(i:windowSize+i-1,:);
%     Y_training = Y(i:windowSize+i-1);
    X_training = X(1:windowSize+i-1,:);
    Y_training = Y(1:windowSize+i-1);
    ctree1 = fitctree(X_training,Y_training,'CategoricalPredictors',size(X,2),'PredictorNames',namePredictors); 
%     newTree2 = fitctree(X_training,Y_training,'MinLeaf',10,'CategoricalPredictors',size(X,2),'PredictorNames',namePredictors); 
%     view(ctree1,'mode','graph') % text description
    resuberror1 = resubLoss(ctree1,'subtrees','all');
    ctree2 = fitctree(X_training,Y_training,'MinLeaf',8,'CategoricalPredictors',size(X,2),'PredictorNames',namePredictors); 
    [E1,SE1,Nleaf1,BestLevel1] = cvLoss(ctree2,'subtrees','all');
    newTree2 = prune(ctree2,'Level',BestLevel1);
%     [resuberror1 E1]
%     if(E1(1) > E1(2))
%         newTree1 = prune(ctree1,'Level',1);
%     else
        newTree1 = ctree1;
       
%     end
    X_predictor = X(i+windowSize,:);
    [Y_predict, Y_score] = predict(newTree1,X_predictor);
    result_predict(i,1) = Y_predict;
    
    [Y_predict2, Y_score2] = predict(newTree2,X_predictor);
    result_predict(i,2) = Y_predict2;
%     [resuberror1 E1]
end
% [Y(windowSize+1:end) result_predict]
% sum(Y(windowSize+1:end)==result_predict(:,1))./(N-windowSize)
figure(3)
Y2 = floor(Y(windowSize+1:end)*100)/100 ;
Y2_f = floor(result_predict(1:end,1)*100)/100;
Y2_f2 = floor(result_predict(1:end,2)*100)/100;

[Y2 Y2_f Y2_f2]
r = ksr(dates2,Y2,10);
plot(dates2,Y2,'ro')
hold all
plot(r.x,r.f,'black--')
plot(dates2,Y2_f,'b')
plot(dates2,Y2_f2,'g')
title('Forecast vs actual interest rate growth')
legend('actual r%', 'smooth r% (kernel n=10)','forecast r(no min leaf)','min leaf')
hy = graph2d.constantline(0, 'Color',[.7 .7 .7]);
changedependvar(hy,'y');
datetick('x',12)
hold off